<template>
  <el-dialog
    :title="dialogProps.title"
    :visible.sync="dialogProps.visible"
    width="560px"
    @open="handleDialogOpen()"
  >
    <div slot="title" class="dialog-header">{{ dialogProps.title }}</div>
    <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
      <el-tab-pane label="基本信息" name="first"></el-tab-pane>
      <el-form
        :model="SportModel"
        :rules="formSport"
        ref="sportForm"
        label-width="80px"
        label-position="right"
        size="small"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="portName">
              <el-input
                ref="portName"
                v-model="SportModel.portName"
                autofocus
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="运动强度" prop="portStrength"
              ><el-input
                ref="portStrength"
                v-model="SportModel.portStrength"
              ></el-input> </el-form-item
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="运动时间" prop="portTime">
              <el-input
                ref="portTime"
                v-model="SportModel.portTime"
                autofocus
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="12">
            <el-form-item label="适用性别" prop="portSex">
              <el-select v-model="SportModel.portSex" placeholder="不限">
                <el-option
                  ref="portSex"
                  v-for="item in apply"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="年龄上限" prop="portUp">
              <el-input
                ref="portUp"
                type="number"
                min="1"
                max="100"
                v-model="SportModel.portUp"
                autofocus
              ></el-input></el-form-item
          ></el-col>

          <el-col :span="12">
            <el-form-item label="年龄下限" prop="portDown"
              ><el-input
                type="number"
                min="1"
                max="100"
                ref="portDown"
                :maxlength="32"
                v-model="SportModel.portDown"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="千步当量" prop="portLent">
              <el-input
                ref="portLent"
                v-model="SportModel.portLent"
                autofocus
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="12">
            <el-form-item label="拉伸部位" prop="portPart">
              <el-select v-model="SportModel.portPart" placeholder="不限">
                <el-option
                  ref="portPart"
                  v-for="item in stretch"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>

        <el-form-item label="运动方法" prop="portWay">
          <el-input
            ref="portWay"
            resize="none"
            type="textarea"
            v-model="SportModel.portWay"
          ></el-input>
        </el-form-item>

        <el-form-item label="注意事项" prop="portMatter">
          <el-input
            ref="portMatter"
            type="textarea"
            resize="none"
            v-model="SportModel.portMatter"
          ></el-input>
        </el-form-item>
      </el-form>
    </el-tabs>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size="small" @click="submitForm('sportForm')"
        >保 存</el-button
      >
      <el-button size="small" @click="handleDialogClose()">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addSport, sportUpdate } from "@/api/Repository/sport";

export default {
  name: "role-edit",
  data() {
    return {
      activeName: "first",

      apply: [
        { value: "男", label: "男" },
        { value: "女", label: "女" }
      ],

      stretch: [
        { value: "上肢", label: "上肢" },
        { value: "下肢", label: "下肢" },
        { value: "腰背", label: "腰背" }
      ],

      SportModel: [
        {
          id: "",
          portName: "",
          portStrength: "",
          portSex: "",
          portUp: "",
          portDown: "",
          portLent: "",
          portPart: "",
          portTime: "",
          portAge: "",
          portWay: "",
          portMatter: ""
        }
      ],

      //加载
      locked: false,

      dialogProps: {
        visible: false,
        action: "",
        title: ""
      },
      formSport: {
        portName: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 2, max: 32, message: "长度在 2 到 32 个字符", trigger: "blur" }
        ],
        portStrength: [
          { required: true, message: "请输入运动强度", trigger: "blur" }
        ],
        portTime: [
          { required: true, message: "请输入运动时间", trigger: "blur" }
        ],
        portSex: [{ required: true, message: "请选择性别", trigger: "change" }],
        portUp: [
          { required: true, message: "请输入年龄上限", trigger: "blur" }
        ],
        portDown: [
          { required: true, message: "请输入年龄下限", trigger: "blur" }
        ],
        portLent: [
          { required: true, message: "请输入千步当量", trigger: "blur" }
        ]
        // portPart: [
        //   { required: true, message: "请选择拉伸部位", trigger: "change" }
        // ]
        // portWay: [
        //   { required: true, message: "请输入运动方法", trigger: "blur" }
        // ],
        // portMatter: [
        //   { required: true, message: "请输入注意事项", trigger: "blur" }
        // ]
      }
    };
  },
  methods: {
    //选项卡点击
    handleClick(tab, event) {
      console.log(tab, event);
    },

    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.dialogProps.action === "add") {
            this.doSaveSport();
          } else {
            this.doUpdateSport();
          }
        } else {
          return false;
        }
      });
    },
    doUpdateSport() {
      sportUpdate(this.SportModel)
        .then(response => {
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "修改成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("修改失败");
        });
    },
    doSaveSport() {
      addSport(this.SportModel)
        .then(response => {
          this.locked = false;
          this.dialogProps.visible = false;
          if (response.data > 0) {
            this.$emit("save-finished");
            this.$message({
              message: "添加成功",
              type: "success"
            });
          }
        })
        .catch(error => {
          this.$message.error("添加失败");
        });
    },

    handleDialogClose() {
      this.dialogProps.visible = false;
    },

    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs["sportForm"].clearValidate();
        this.$refs["sportName"].focus();
      });
    },

    outputError(error) {
      console.log(error.response ? error.response : error.message);
      this.$message({
        showClose: true,
        message: "出错了，请按F12查看浏览器日志。",
        type: "error"
      });
    }
  },
  mounted: function() {
    this.$nextTick(() => {
      this.$on("openEditSportDialog", function(eModel) {
        this.SportModel = eModel;
        this.dialogProps.action = "edit";
        this.dialogProps.title = "编辑";
        this.dialogProps.visible = true;
      });
      this.$on("openAddSportDialog", function() {
        this.dialogProps.action = "add";
        this.dialogProps.title = "新增";
        this.SportModel = {};
        this.dialogProps.visible = true;
      });
    });
  }
};
</script>

